package com.example.mongodb.controller;

import com.example.mongodb.entity.DishesInfo;
import com.example.mongodb.entity.RestaurantInfo;
import com.example.mongodb.mongodb.RestaurantDoc;
import com.example.mongodb.mongodb.RestaurantDocService;
import com.example.mongodb.service.DishesService;
import com.example.mongodb.service.RestaurantService;
import org.springframework.data.domain.Page;
import org.springframework.data.geo.GeoResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-5-16 19:35
 * @description :
 */

@RestController
@RequestMapping("/api")
public class RestaurantController {
    @Resource
    private RestaurantService restaurantService;
    @Resource
    private RestaurantDocService restaurantDocService;
    @Resource
    private DishesService dishesService;

    @PostMapping("/restaurant-infos")
    public ResponseEntity createRestaurant(@RequestBody RestaurantInfo restaurantInfo) {
        RestaurantInfo result = restaurantService.addRestaurant(restaurantInfo);
        if (result == null) {
            return ResponseEntity.badRequest().body("创建餐厅失败");
        }
        return ResponseEntity.ok(result);
    }

    @PutMapping("/restaurant-infos")
    public ResponseEntity updateRestaurant(@RequestBody RestaurantInfo restaurantInfo) {
        RestaurantInfo result = restaurantService.updateRestaurant(restaurantInfo);
        if (result == null) {
            return ResponseEntity.badRequest().body("修改餐厅失败");
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/restaurant-infos/{id}")
    public ResponseEntity deleteRestaurant(@PathVariable Long id) {
        Boolean result = restaurantService.deleteRestaurant(id);
        if (result) {
            return ResponseEntity.ok("删除餐厅成功");
        }
        return ResponseEntity.badRequest().body("删除餐厅失败");
    }

    @PostMapping("/dishes-infos")
    public ResponseEntity createDishes(@RequestBody DishesInfo dishesInfo) {
        DishesInfo result = dishesService.addDishes(dishesInfo);
        if (result == null) {
            return ResponseEntity.badRequest().body("创建菜品失败");
        }
        return ResponseEntity.ok(result);
    }

    @PutMapping("/dishes-infos")
    public ResponseEntity updateDishes(@RequestBody DishesInfo dishesInfo) {
        DishesInfo result = dishesService.addDishes(dishesInfo);
        if (result == null) {
            return ResponseEntity.badRequest().body("更新菜品失败");
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/dishes-infos/{id}")
    public ResponseEntity deleteDishes(@PathVariable Long id) {
        Boolean result = dishesService.deleteDishes(id);
        if (result) {
            return ResponseEntity.ok("删除菜品成功");
        }
        return ResponseEntity.badRequest().body("删除菜品失败");
    }

    /**
     * 关键字分页检索美食库
     * 
     * @param keywords 关键字
     * @param page 第几页
     * @param size 每页数量
     * @return
     */
    @GetMapping("/restaurant-mongo/search")
    public ResponseEntity getAllRestaurants(String keywords, Integer page, Integer size) {
        Page<RestaurantDoc> results = restaurantDocService.getRestaurantsByKeywords(keywords, page, size);
        return ResponseEntity.ok(results);
    }

    @GetMapping("/restaurant-mongo/location")
    public ResponseEntity getNearRestaurants(Double lon, Double lat, Long distance) {
        List<GeoResult<RestaurantDoc>> results = restaurantDocService.getRestaurantsByPosition(lon, lat, distance);
        return ResponseEntity.ok(results);
    }
}
